<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
     <!-- 卡片 -->
    <el-card>
      <!-- 头部搜索 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="params.query" clearable @clear="getGoodsList">
          <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" class="btn" @click="addgoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
       <el-table
      :data="GoodsList"
      style="width: 100%"
      border
      row-key="goods_id">
      <el-table-column prop="id" label="#" type="index"></el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称" >
      </el-table-column>
      <el-table-column
        prop="goods_price"
        label="商品价格" width="95px">
      </el-table-column>
      <el-table-column
        prop="goods_weight"
        label="商品重量" width="70px">
      </el-table-column>
      <el-table-column
        prop="add_time"
        label="创建时间" width="160px">
        <template slot-scope="scope">
          {{scope.row.add_time | dateFormat}}
            </template>
      </el-table-column>
      <el-table-column
        prop="mg_state"
        label="操作" width="130px">
        <template slot-scope="data">
        <div class="bt">
          <el-button type="primary" size="mini"  icon="el-icon-edit"  @click="index(data.row.goods_id)"></el-button>
        <el-button type="danger"   size="mini"  icon="el-icon-delete"  @click="del(data.row.goods_id)"></el-button>
        </div>
        </template>
      </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="params.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      params: { // 分页数据和发送数据
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      GoodsList: [], // 商品数据
      total: 0
    }
  },
  methods: {
    async getGoodsList () { // 获取商品信息
      const { data: res } = await this.$http.get('goods', { params: this.params })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.GoodsList = res.data.goods
      this.total = res.data.total
      console.log(this.GoodsList)
    },
    handleSizeChange (newSize) { // 改变页面展示几条数据下拉框获取响应的表格数据
      this.params.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange (newNum) { // 改变页码获取响应的表格数据
      this.params.pagenum = newNum
      this.getGoodsList()
    },
    addgoods () { // 添加商品
      this.$router.push('/goods/add')
      sessionStorage.setItem('avtivePath', '/goods/add')
    },
    async del (id) {
      const datas = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).catch(res => res) // 通过catch捕获错误并且返回res
      if (datas === 'cancel') return false
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message({ message: res.meta.msg, type: 'success' })
      this.getGoodsList()
    }
  },
  created () {
    this.getGoodsList()
  }
}
</script>

<style lang="less" scoped>
.btn {
  margin-left: 10px;
}
.bt {
  display: flex;
}
</style>
